<template>
  <div id="nav">
    <van-nav-bar title="胡说八道(9)">
      <template #right>
        <van-popover
          v-model:show="nav_more_popver.display"
          :actions="nav_more_popver.actions"
          @select="nav_more_popver.onSelect"
          placement="bottom-end"
        >
          <template #reference>
            <van-icon name="weapp-nav" size="20" />
          </template>
        </van-popover>
      </template>
      <template #left></template>
    </van-nav-bar>
  </div>
  <div class="content">
    <Message v-if="tabbar_active == 0" @change="tabbar_chat_unread.change" />
    <Friends v-if="tabbar_active == 1" />
    <More v-if="tabbar_active == 2" />
  </div>
  <div class="tabbar">
    <van-tabbar v-model="tabbar_active">
      <van-tabbar-item icon="chat" :badge="tabbar_chat_unread.num">消息</van-tabbar-item>
      <van-tabbar-item icon="friends">朋友</van-tabbar-item>
      <van-tabbar-item icon="more">发现</van-tabbar-item>
      <van-tabbar-item icon="manager">我</van-tabbar-item>
    </van-tabbar>

    <div class="menu">
      <div class="add_friend_view">
        <van-action-sheet
          v-model:show="add_friend_view.display"
          title="添加好友"
          :close-on-click-overlay="false"
        >
          <div class="search">
            <van-search
              v-model="add_friend_view.searchText"
              show-action
              placeholder="根据昵称搜索"
              @search="add_friend_view.onSearch"
              @cancel="add_friend_view.display = false"
            ></van-search>
          </div>
          <div class="searchResults van-hairline--top">
            <!-- searchResult -->
            <div class="noData" v-if="add_friend_view.searchResult.length == 0">
              <van-empty image="search" description="没有找到这个用户呢" />
            </div>
            <div class="items" v-else>
              <div
                class="item van-hairline--surround"
                v-for="item in add_friend_view.searchResult"
                :key="item.id"
              >
                <van-card desc="item.lastMsg" :title="item.nickName">
                  <template #thumb>
                    <van-badge>
                      <div class="child">
                        <img :src="item.faceImage" />
                      </div>
                    </van-badge>
                  </template>
                  <template #desc>
                    <div class="tag">
                      <van-tag
                        color="#ffe1e1"
                        text-color="#ad0000"
                        size="large"
                      >{{(item.sex == '0' ? '男' : '女')}}</van-tag>
                      <van-tag
                        color="#7232dd"
                        size="large"
                        style="margin-left: 10px;"
                      >胡说号:{{item.id}}</van-tag>
                    </div>
                  </template>
                  <template #num>
                    <van-button plain type="info">申请</van-button>
                  </template>
                </van-card>
              </div>
            </div>
          </div>
        </van-action-sheet>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {
  sendMessage,
  MessageActionType,
  WebSocketInit,
} from "@/service/WsService";
import { onMounted, reactive, ref } from "vue";
import { getFriendData, getUserDataCache, toLogin } from "@/utils/app";
import { getUserId } from "@/utils/app";
import { Notify } from "vant";
import { Toast } from "vant";
import { Options } from "vue-class-component";
import Friends from "@/components/Friends.vue";
import Message from "@/components/Message.vue";
import More from "@/components/More.vue";
import { searchFriendsByNickName } from "@/api/requests/base";

export default {
  components: {
    Friends,
    Message,
    More,
  },
  setup() {
    onMounted(() => {
      // 获得好友数据
      WebSocketInit();
      getFriendData();
    });

    const tabbar_active = ref(0);
    const tabbar_chat_unread = reactive({
      num: 0,
      change: (val: number) => {
        tabbar_chat_unread.num = val;
      },
    });
    const nav_more_popver = ref({
      display: false,
      actions: [
        { text: "添加好友" },
        { text: "添加聊天群" },
        { text: "我的二维码" },
      ],
      onSelect: (action: any) => {
        Toast();
        if (action.text == "添加好友") {
          add_friend_view.display = true;
        }
      },
    });
    const add_friend_view = reactive({
      display: false,
      searchText: "",
      searchResult: [],
      onSearch: () => {
        searchFriendsByNickName(add_friend_view.searchText).then((res: any) => {
          add_friend_view.searchResult = res.data;
        });
      },
    });
    return {
      tabbar_active,
      tabbar_chat_unread,
      nav_more_popver,
      add_friend_view,
    };
  },
};
</script>

<style lang="less" scoped>
.content {
  margin: 5px 10px;
  height: 80vh;
  background-color: #f7f8fa;
}

.searchResults {
  margin: 10px;
  padding: 10px;
  height: 50vh;
  .items {
    background-color: #f7f8fa;
    height: 50vh;
    overflow: auto;
  }
  .item {
    height: 60px;
    .van-card {
      height: 45px;
    }
    .van-tag {
      margin-top: 7px;
    }
    .van-button {
      position: absolute;
      top: 1px;
      right: 1px;
    }
    .van-badge__wrapper {
      position: absolute;
      left: 38px;
      .child {
        img {
          border-radius: 0px;
        }
      }
    }
  }
}
.menu {
  .add_friend_view {
    height: 50vh;
    padding: 15px;
  }
}

.child {
  width: 40px;
  height: 40px;
  background: #f2f3f5;
  border-radius: 4px;
}
</style>
